package com.zz.xdialog.base

import android.content.DialogInterface
import android.os.Parcel
import android.os.Parcelable
import android.view.Gravity
import androidx.fragment.app.FragmentManager
import androidx.viewbinding.ViewBinding
import com.zz.xdialog.listener.OnBindViewListener
import kotlinx.android.parcel.Parcelize
import java.io.Serializable

/**
 * 数据保存封装的容器类
 * Copyright © 2025 zhun All rights reserved.
 * @author: Zha0Zhun 1820022519@qq.com
 * @version: V1.0
 */
class XController : Serializable {
    var fragmentManager: FragmentManager? = null
    var height: Int = 0
    var width: Int = 0
    var dimAmount: Float = 0.2f
    var gravity: Int = Gravity.CENTER
    var tag: String = "XDialog"
    var isCancelableOutside: Boolean = true
    var onBindViewListener: OnBindViewListener<*>? = null
    var dialogAnimationRes: Int = 0
    var viewBindingClass: Class<out ViewBinding>? = null
    var onDismissListener: DialogInterface.OnDismissListener? = null
    var onKeyListener: DialogInterface.OnKeyListener? = null

    fun toParcelable(): XParams {
        return XParams(
            height, width, dimAmount, gravity, tag, isCancelableOutside, dialogAnimationRes
        )
    }

    fun restoreFromParcelable(data: XParams) {
        height = data.height
        width = data.width
        dimAmount = data.dimAmount
        gravity = data.gravity
        tag = data.tag
        isCancelableOutside = data.isCancelableOutside
        dialogAnimationRes = data.dialogAnimationRes
    }

    @Parcelize
    data class XParams(
        var width: Int = 0,
        var height: Int = 0,
        var dimAmount: Float = 0.2f,
        var gravity: Int = Gravity.CENTER,
        var tag: String = "XDialog",
        var isCancelableOutside: Boolean = true,
        var dialogAnimationRes: Int = 0
    ):Parcelable {
        var bindViewListener: OnBindViewListener<*>? = null
        var viewBindingClass: Class<out ViewBinding>? = null
        var fragmentManager: FragmentManager? = null
        var onDismissListener: DialogInterface.OnDismissListener? = null
        var onKeyListener: DialogInterface.OnKeyListener? = null
        fun apply(controller: XController) {
            controller.fragmentManager = fragmentManager
            controller.width = width
            controller.height = height
            controller.dimAmount = dimAmount
            controller.gravity = gravity
            controller.tag = tag
            controller.isCancelableOutside = isCancelableOutside
            controller.onBindViewListener = bindViewListener
            controller.dialogAnimationRes = dialogAnimationRes
            controller.viewBindingClass = viewBindingClass
            controller.onDismissListener = onDismissListener
            controller.onKeyListener = onKeyListener
        }
    }
}